package cn.iocoder.yudao.module.promotion.redpacket.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.annotation.AppJsonView;
import cn.iocoder.yudao.framework.business.basic.controller.PageController;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.promotion.redpacket.pojo.dto.RedPacketRecordQueryDTO;
import cn.iocoder.yudao.module.promotion.redpacket.pojo.vo.RedPacketListVO;
import cn.iocoder.yudao.module.promotion.redpacket.pojo.vo.RedPacketRecordVO;
import cn.iocoder.yudao.module.promotion.redpacket.service.app.AppRedPacketRecordService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 前端控制器
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/06/21
 */
@Tag(name = "用户 APP - 用户红包记录")
@RestController
@RequestMapping("/promotion/redpacket/record")
public class AppRedPacketRecordController implements PageController<RedPacketRecordVO, RedPacketRecordQueryDTO> {
    @Resource
    private AppRedPacketRecordService appRedPacketRecordService;

    @Resource
    private ShopInfoApi shopInfoApi;

    @JsonView(AppJsonView.class)
    @Override
    public CommonResult<PageResult<RedPacketRecordVO>> getPage(Integer pageNo, Integer pageSize, RedPacketRecordQueryDTO param) {
        return CommonResult.success(appRedPacketRecordService.getPage(pageNo, pageSize, param));
    }

    @GetMapping("/page-group")
    @Operation(summary = "分页（按店铺分组）")
    public CommonResult<PageResult<RedPacketListVO>> getGroupPage(Integer pageNo, Integer pageSize, RedPacketRecordQueryDTO param) {
        PageResult<RedPacketRecordVO> page = appRedPacketRecordService.getPage(pageNo, pageSize, param, getLoginUserId());
        List<RedPacketRecordVO> list = page.getList();
        if (CollUtil.isEmpty(list)) {
            return success(PageResult.empty());
        }
        Map<Long, ShopInfoDTO> shops = shopInfoApi.getShopInfoMap(convertSet(list, RedPacketRecordVO::getShopId));
        // 按店铺分组
        Map<Long, List<RedPacketRecordVO>> groupByShop = new HashMap<>();
        for (RedPacketRecordVO it : list) {
            groupByShop.computeIfAbsent(it.getShopId(), k -> new ArrayList<>()).add(it);
        }
        // 转换数据格式
        List<RedPacketListVO> respVOS = new ArrayList<>();
        for (Map.Entry<Long, List<RedPacketRecordVO>> entry : groupByShop.entrySet()) {
            RedPacketListVO respVO = new RedPacketListVO();
            respVO.setShopId(entry.getKey());
            respVO.setShop(shops.get(entry.getKey()));
            respVO.setItems(entry.getValue());
            respVOS.add(respVO);
        }
        return success(new PageResult<>(respVOS, page.getTotal()));
    }

    @GetMapping("/granted")
    @Parameter(name = "tradeNo", description = "交易单号")
    @Operation(summary = "根据交易单号查询已发放的红包金额")
    @PermitAll // 无需登录限制，扫码商家码付款的情况下可能未登录
    public CommonResult<Integer> getGrantedAmount(@RequestParam(value = "tradeNo") String tradeNo) {
        return success(appRedPacketRecordService.getGrantedRedPacket(tradeNo));
    }

}
